#include "my_spi.h"

/**
 * @brief 硬件SPI_GPIO初始化
 * 
 */
void SPI_GPIO_Init(void)
{
    SPI_InitTypeDef   SPI_InitStructure;
	GPIO_InitTypeDef  GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
	
	//配置SPI的SCK，MISO和MOSI引脚为复用推挽模式
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);

	SPI_InitStructure.SPI_Mode=SPI_Mode_Master;	//配置为主机模式
	SPI_InitStructure.SPI_NSS=SPI_NSS_Soft;		//NSS软件管理
	SPI_InitStructure.SPI_CPHA=SPI_CPHA_1Edge;	//第一个时钟沿捕获
	SPI_InitStructure.SPI_CPOL=SPI_CPOL_Low;	//空闲状态为低电平
	SPI_InitStructure.SPI_DataSize=SPI_DataSize_8b;						//8位数据帧
	SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_8; 	//SPI波特率8分频 	36/8=4.5M
	SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;	//全双工模式
	SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB;					//数据高位先行
	SPI_InitStructure.SPI_CRCPolynomial=7;								//CRC计算多项式
	SPI_Init(SPI2,&SPI_InitStructure);
	
	SPI_Cmd(SPI2,ENABLE);	//SPI2使能
}

/**
 * @brief SPI 写一个字节数据
 * 
 * @param data 要写入的字节
 * @return uint8_t 
 */
uint8_t SPI_Write_Read_Byte(uint8_t data)
{
    while(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE));
	 SPI_I2S_SendData(SPI2, data);
	
	 while(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE));
	 return SPI_I2S_ReceiveData(SPI2);
}
